﻿namespace StudentManager.API.BLL
{
    public enum ResultCode
    {
        Success = 0, // 成功
        Error = -1 // 失败
    }
    public interface IResult
    {
        /// <summary>
        /// 状态码
        /// </summary>
        int code { get; set; }
        /// <summary>
        /// 
        /// </summary>
        string? msg { get; set; }
        /// <summary>
        /// 数据
        /// </summary>
        object? data { get; set; }
    }

    public class Result: IResult
    {
        public int code { get; set; }
        public string? msg { get; set; }
        public object? data { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="data"></param>
        /// <param name="code"></param>
        /// <param name="msg"></param>
        public Result(object? data = null, 
            ResultCode code = ResultCode.Success, 
            string? msg = null)
        {
            this.code = (int)code;
            this.msg = msg;
            this.data = data;
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static Result OK(string ? msg = null, 
            object? data = null)
        {
            return new Result(data, ResultCode.Success, msg);
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static Result Fail(string? msg = null)
        {
            return new Result(null, ResultCode.Error, msg);
        }

        /// <summary>
        /// 判断操作是否成功
        /// </summary>
        /// <param name="isOk"></param>
        /// <returns></returns>
        public static Result IsOk(bool isOk)
        {
            return new Result(null, isOk ? ResultCode.Success : ResultCode.Error);
        }

        /// <summary>
        /// 带数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static Result Data(object? data)
        {
            return new Result(data, data == null ? ResultCode.Error : ResultCode.Success);
        }
    }
}
